<?php


//***************************************************************************************************_______________U2Vaght


function U2Vaght( $U ){

	$TIME  = U2Time($U);

	$vaght = Time2Vaght($TIME);	

	return $vaght;

}


//***************************************************************************************************_______________MLD_2_SMSI


function MLD_2_SMSI( $TIME ){


	$ALL_DAYS = 0;


	$MONTH = 0;

	$Y = substr($TIME, 2, 2);


	$M = substr($TIME, 5, 2);


	$D = substr($TIME, 8, 2);


	$E = substr($TIME, 11,8);


	switch( $M ){


		case 2	: {$MONTH = 31;	break;}


		case 3	: {$MONTH = 59;	break;}


		case 4	: {$MONTH = 90;	break;}

		case 5	: {$MONTH = 120;	break;}


		case 6	: {$MONTH = 151;	break;}


		case 7	: {$MONTH = 181;	break;}		

		case 8	: {$MONTH = 212;	break;}


		case 9	: {$MONTH = 243;	break;}


		case 10	: {$MONTH = 273;	break;}		

		case 11	: {$MONTH = 304;	break;}


		case 12	: {$MONTH = 334;	break;}


	}

	$ALL_DAYS = $MONTH + $D + 285 ;

	$WKDY = ($ALL_DAYS+2)%7;

	if($ALL_DAYS>=365) { $Y++; $ALL_DAYS-=365; }

	$Y = $Y + 78 ;

	if( $ALL_DAYS<=186 ){

		$D = $ALL_DAYS % 31 ;

		$M = ($ALL_DAYS - $D)/31 + 1;

	}

	else if( $ALL_DAYS>186 && $ALL_DAYS<=336 ){

		$ALL_DAYS -=186;

		$D = $ALL_DAYS % 30 ;

		$M = ($ALL_DAYS - $D)/30 + 7;

	}
	else {


		$M = 12;

		$D = $ALL_DAYS - 336;

	}


	$D++;

	//$M++;

	$D = addZiro($D);

	$M = addZiro($M);

	$Y = addZiro($Y);

	$Y = '13' . $Y ;

	$TIME = $Y . '-' . $M . '-' . $D . ' ' . $E . '|' . $WKDY ;

	return $TIME;

}	


	


























//***************************************************************************************************_______________Vaght_2_Taghvim


function Vaght_2_Taghvim( $TIME,$HR=0,$CLEAR=0 ){





	$ALlDATE = array();	





	$TYear 	= substr($TIME, 2, 2);


	$TMont 	= substr($TIME, 5, 2);


	$TDate 	= substr($TIME, 8, 2);





	$E = substr($TIME, 11, 8);


	$TDay =  substr($TIME, 20, 1);





	switch( $TDay ){


		case 0 	:	{$TDAY = 'شنبه';break;}


		case 1 	:	{$TDAY = 'يکشنبه';break;}


		case 2 	:	{$TDAY = 'دوشنبه';break;}


		case 3 	:	{$TDAY = 'سه شنبه';break;}


		case 4 	:	{$TDAY = 'چهارشنبه';break;}


		case 5 	:	{$TDAY = 'پنجشنبه';break;}


		case 6 	:	{$TDAY = 'جمعه';break;}


	}





	switch( $TMont ){


		case '01' 	:	{$TMONT = 'فروردين';break;}


		case '02' 	:	{$TMONT = 'ارديبهشت';break;}


		case '03' 	:	{$TMONT = 'خرداد';break;}


		case '04' 	:	{$TMONT = 'تير';break;}


		case '05' 	:	{$TMONT = 'مرداد';break;}


		case '06'	:	{$TMONT = 'شهريور';break;}


		case '07' 	:	{$TMONT = 'مهر';break;}


		case '08' 	:	{$TMONT = 'آبان';break;}


		case '09' 	:	{$TMONT = 'آذر';break;}


		case '10' 	:	{$TMONT = 'دي';break;}


		case '11'	:	{$TMONT = 'بهمن';break;}


		case '12' 	:	{$TMONT = 'اسفند';break;}


	}








	$TIME = '' . $TDAY  .'&nbsp;'. $TDate . '&nbsp;' . $TMONT . '&nbsp;' .'13'. $TYear;


	if($HR)$TIME .= ' ساعت ' . $E;


	


	if(!$CLEAR)$TIME = '<span dir=rtl align=center>&nbsp;'.$TIME;


	if(!$CLEAR)$TIME = $TIME.'</span>';





		


	


	return $TIME;


}














//***************************************************************************************************_______________Time_2_Date


function Time_2_Date( $TIME ){





	$ALlDATE = array();	





	$TYear 	= substr($TIME, 2, 2);


	$TMont 	= substr($TIME, 5, 2);


	$TDate 	= substr($TIME, 8, 2);





	$E = substr($TIME, 11, 8);


	$TDay =  substr($TIME, 20, 1);





	switch( $TDay ){


		case 0 	:	{$TDAY = 'Saturday';break;}


		case 1 	:	{$TDAY = 'Sunday';break;}


		case 2 	:	{$TDAY = 'Monday';break;}


		case 3 	:	{$TDAY = 'Tuesday';break;}


		case 4 	:	{$TDAY = 'Wednesday';break;}


		case 5 	:	{$TDAY = 'Thursday';break;}


		case 6 	:	{$TDAY = 'Friday';break;}


	}





	switch( $TMont ){


		case '01' 	:	{$TMONT = 'January';break;}


		case '02' 	:	{$TMONT = 'February';break;}


		case '03' 	:	{$TMONT = 'March';break;}


		case '04' 	:	{$TMONT = 'April';break;}


		case '05' 	:	{$TMONT = 'May';break;}


		case '06'	:	{$TMONT = 'June';break;}


		case '07' 	:	{$TMONT = 'July';break;}


		case '08' 	:	{$TMONT = 'August';break;}


		case '09' 	:	{$TMONT = 'September';break;}


		case '10' 	:	{$TMONT = 'October';break;}


		case '11'	:	{$TMONT = 'November';break;}


		case '12' 	:	{$TMONT = 'December';break;}


	}





	$TIME = '<span align=center class=TX1>&nbsp;' . $TDAY  .'&nbsp; '. $TDate . '&nbsp;' . $TMONT . '&nbsp;20' . $TYear . ' Time: ' . $E . '</span>';





	return $TIME;


}


//***************************************************************************************************_______________Vaght2U


function Vaght2U( $TIME ){


	$Y = substr($TIME, 2, 2 );


	$M = substr($TIME, 5, 2 );


	$D = substr($TIME, 8, 2 );

	$H = substr($TIME, 11, 2);


	$I = substr($TIME, 14, 2);


	$S = substr($TIME, 17, 2);

	switch( $M )	{


		case 1	: {$MONTH = 0;	break;}//farvardin


		case 2	: {$MONTH = 31;	break;}//ordibehesh


		case 3	: {$MONTH = 62;	break;}//khordad


		case 4	: {$MONTH = 93;	break;}//tirr		


		case 5	: {$MONTH = 124;	break;}//mordad


		case 6	: {$MONTH = 155;	break;}//shahrivar


		case 7	: {$MONTH = 186;	break;}//mehrr

		case 8	: {$MONTH = 216;	break;}//aban


		case 9	: {$MONTH = 246;	break;}//azarr


		case 10	: {$MONTH = 276;	break;}//dey	

		case 11	: {$MONTH = 306;	break;}//bahman


		case 12	: {$MONTH = 336;	break;}//esfand


	}


	$D = $MONTH + $D;


	$Y -= 49;





	$U += $S ;


	$U += $I * 60 ;


	$U += $H * 60 * 60 ;


	$U += $D * 24 * 60 * 60 ;


	$U += $Y * 31554850 ;


	


	$U += 6844450;


	


	$TIME = $U ;





	return $TIME;


}


//<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>__________________Time2U


function Time2U($T){





$Y = substr($T, 0, 4);


$m = substr($T, 5, 2);


$d = substr($T, 8, 2);

$H = substr($T, 11, 2);


$i = substr($T, 14, 2);


$s = substr($T, 17, 2);

switch( $m ){


	case '01'	:{ $d+=0; break;}


	case '02'	:{ $d+=31; break;}


	case '03'	:{ $d+=59; break;}


	case '04'	:{ $d+=90; break;}


	case '05'	:{ $d+=120; break;}


	case '06'	:{ $d+=151; break;}


	case '07'	:{ $d+=181; break;}


	case '08'	:{ $d+=212; break;}


	case '09'	:{ $d+=243; break;}


	case '10'	:{ $d+=273; break;}


	case '11'	:{ $d+=304; break;}


	case '12'	:{ $d+=334; break;}


}

$S = ($Y - 1970) * 31554850  +  $d*3600*24  +  $H*3600  +  $i*60  +  $s  ;


if( ($m>03 || ($m==3 && substr($T, 8, 2)>20)) && $m<9) $S-=3600;

return $S;


}


//<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>__________________U2Time


function U2Time($U){


	$d=0;$m=0;$Y=0;

	$U = FIX_U($U);


	$Y = (($U-($U%31554850))/31554850)+1970;


	$U = $U % 31554850;

	$s = $U % 60 ;


	$U = $U - $s;

	$i = ($U % 3600)/60;


	$U = $U - $i * 60;

	$H = ($U % 86400)/3600;


	$U = $U - $H * 3600;

	$d = $U / 86400;

	$W=($Y*365+$d+3)%7;

	if($d>334){	$d-=334; $m=12; }else


	if($d>304){	$d-=304; $m=11; }else


	if($d>273){	$d-=273; $m=10; }else


	if($d>243){	$d-=243; $m=9 ; }else


	if($d>212){	$d-=212; $m=8 ; }else


	if($d>181){	$d-=181; $m=7 ; }else


	if($d>151){	$d-=151 ; $m=6 ; }else


	if($d>120){	$d-=120 ; $m=5 ; }else


	if($d>90 ){	$d-=90 ; $m=4 ; }else


	if($d>59 ){	$d-=59 ; $m=3 ; }else


	if($d>31 ){	$d-=31 ; $m=2 ; }else


	$m = 1;

//if( ($m>03 || ($m==3 && $d>20)) && $m<9) $H++;


	return  $Y.'.'.addZiro($m).'.'.addZiro($d).' '.addZiro($H%24).":".addZiro($i).":".addZiro($s)."|".$W;


}


//<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>__________________Time2Vaght


function Time2Vaght( $TIME ){


	$Y = substr($TIME ,2 ,2);


	$M = substr($TIME ,5 ,2);


	$D = substr($TIME ,8 ,2);	

	$E = substr($TIME ,11 ,8);


	$W = substr($TIME ,20 ,1);

	switch( $M ){


		case 2	: $MONTH = 31;	break;

		case 3	: $MONTH = 59;	break;

		case 4	: $MONTH = 90;	break;

		case 5	: $MONTH = 120;	break;

		case 6	: $MONTH = 151;	break;

		case 7	: $MONTH = 181;	break;

		case 8	: $MONTH = 212;	break;


		case 9	: $MONTH = 243;	break;


		case 10	: $MONTH = 273;	break;

		case 11	: $MONTH = 304;	break;


		case 12	: $MONTH = 334;	break;


	}


	$ALL_DAYS = $MONTH + $D + 285 ;

	$WKDY = ($ALL_DAYS+1) % 7;

	if($ALL_DAYS>=365) { $Y++; $ALL_DAYS-=365; }

	$Y += 78 ;

	if( $ALL_DAYS<=186 ){


		$D = $ALL_DAYS % 31 ;


		$M = ($ALL_DAYS - $D)/31 + 1; }

	else if( $ALL_DAYS>186 && $ALL_DAYS<=336 ){


		$ALL_DAYS -=186;


		$D = $ALL_DAYS % 30 ;


		$M = ($ALL_DAYS - $D)/30 + 7;	}


	else {


		$M = 12;


		$D = $ALL_DAYS - 336 ; }

	$D++;


	//$M++;

	$D = addZiro($D);


	$M = addZiro($M);


	$Y = addZiro($Y);


	$Y = '13' . $Y ;

	$TIME = $Y . '.' . $M . '.' . $D . ' ' . $E . '|' . $W ;

	return $TIME;





}


//<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>__________________addZiro


function addZiro($Q){if($Q<10)return '0'.$Q;else return $Q;}


//<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>__________________FIX_U


function FIX_U($U,$LEX=0){


	$xd = $U;


	$xd = $xd%31554850;


	$xd -= $xd%60;


	$xd -= $xd%3600;


	$xd -= $xd%86400;


	$xd /= 86400;


	if(($xd<243)&&($xd>79))


		if($LEX)$U-=3600;


		else $U+=3600;


	return $U;


}


//<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>__________________U2U


function U2U($X='',$U){


	$U = FIX_U($U);

	$EE = $U;

	$Y = $U-($U%31554850);//.


	$U = $U%31554850;


	$s = $U%60 ;//.


	$U = $U - $U%60;


	$i = $U%3600;//.


	$U = $U - $U%3600;


	$H = $U%86400;//.


	$U = $U - $U%86400;


	$d = $U;//.

	for($F=0; $F<strlen($X); $F++){


		$K = substr($X,$F,1);


		if($K=='m' || $K=='d')return false;


		switch($K){


			case 'Y' :


				$EE -= $Y;


				break;


			case 'H' :


				$EE -= $H;


				break;


			case 'i' :


				$EE -= $i;


				break;


			case 's' :


				$EE -= $s;


				break;


		}


	}


	$EE = FIX_U($EE,1);


	return $EE;


}
